﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASCalpuff.FileLayers
{
    public class PG14:Group
    {
        //面源数量
        int m_NAR1 = 0;

        public int NAR1
        {
            get { return m_NAR1; }
            set { m_NAR1 = value; }
        }

        //面源排放速率单位
        //1 =        g/m**2/s
        //   2 =       kg/m**2/hr
        //   3 =       lb/m**2/hr
        //   4 =     tons/m**2/yr
        //   5 =     Odour Unit * m/s  (vol. flux/m**2 of odour compound)
        //   6 =     Odour Unit * m/min
        //   7 =     metric tons/m**2/yr
        int m_IARU = 1;

        public int IARU
        {
            get { return m_IARU; }
            set { m_IARU = value; }
        }


        //14d中提供的具有可变排放速率因子的污染物物种的数量
        int m_NSAR1 = 0;

        public int NSAR1
        {
            get { return m_NSAR1; }
            set { m_NSAR1 = value; }
        }

        //具有任意变化的排放参数的浮力面源的数量
        int m_NAR2 = 0;

        public int NAR2
        {
            get { return m_NAR2; }
            set { m_NAR2 = value; }
        }
        /// <summary>
        /// 面源排放参数
        /// </summary>
        List<AreaSInfo> m_Area = new List<AreaSInfo>();

        public List<AreaSInfo> Area
        {
            get { return m_Area; }
            // /set { m_Area = value; }
        }


      

        public override string WriteToGroup()
        {
            string Subgroup = "", TempStr;

            TempStr = StrWriter.GetCLine();
            TempStr += "INPUT GROUPS: 14a, 14b, 14c, 14d -- Area source parameters" + "\r\n";
            Subgroup += TempStr;

            TempStr = StrWriter.GetCLine();
            Subgroup += TempStr;

            TempStr = "! NAR1 = " + NAR1.ToString() + " !" + "\r\n";
            Subgroup += TempStr;

            TempStr = "! IARU = " + IARU.ToString() + " !" + "\r\n";
            Subgroup += TempStr;

            TempStr = "! NSAR1 = " + NSAR1.ToString() + " !" + "\r\n";
            Subgroup += TempStr;

            TempStr = "! NAR2 = " + NAR2.ToString() + " !" + "\r\n";
            Subgroup += TempStr;

            Subgroup += "!END!" + "\r\n";



            //进行排序
            List<string> arry = Sort.GetPollutantRightOrder();




            List<AreaSInfo> arryAreaSInfo = new List<AreaSInfo>();
            arryAreaSInfo.Clear();
            foreach (AreaSInfo A in Area)
            {
                arryAreaSInfo.Add((AreaSInfo)A.Clone());
            }
            List<PolluteRatesVar> arryPolluteRatesVar = new List<PolluteRatesVar>();

            for (int i = 0; i < Area.Count; i++)
            {
                arryPolluteRatesVar.Clear();
                foreach (string a in arry)
                {
                    for (int j = 0; j < Area[i].X.ArryPolluteRatesVar.Count; j++)
                    {

                        if (a == Area[i].X.ArryPolluteRatesVar[j].PolluteName)
                        {
                            arryPolluteRatesVar.Add(Area[i].X.ArryPolluteRatesVar[j]);
                            break;
                        }
                    }
                }
                for (int k = 0; k < arryAreaSInfo[i].X.ArryPolluteRatesVar.Count; k++)
                {
                    arryAreaSInfo[i].X.ArryPolluteRatesVar[k] = (PolluteRatesVar)arryPolluteRatesVar[k].Clone();
                }
            }

            //---------------------------------------



            if (arryAreaSInfo.Count > 0)
            {
                for (int i = 0; i < arryAreaSInfo.Count; i++)
                {



                    //if (arryAreaSInfo[i].SourceVarHouEmiRatBase.IsSourceHour == false)
                    //{
                    //    TempStr = "! SRCNAM =  " + arryAreaSInfo[i].SRCNAM + "  !" + "\r\n";
                    //    TempStr += "! X =          " + Math.Round(arryAreaSInfo[i].X.EffectHeight, 3).ToString() + ",     " + Math.Round(arryAreaSInfo[i].X.BaseElevation, 3).ToString() + ",     " + Math.Round(arryAreaSInfo[i].X.SigmaZ, 3).ToString();
                    //    double A = 0;
                    //    for (int j = 0; j < arryAreaSInfo[i].X.ArryPolluteRatesVar.Count; j++)
                    //    {
                    //        double p = 1000;
                    //        double X = Math.Abs(Math.Sqrt((arryAreaSInfo[i].X.LowerLeftX - arryAreaSInfo[i].X.LowerRightX) * (arryAreaSInfo[i].X.LowerLeftX - arryAreaSInfo[i].X.LowerRightX) + (arryAreaSInfo[i].X.LowerLeftY - arryAreaSInfo[i].X.LowerRightY) * (arryAreaSInfo[i].X.LowerLeftY - arryAreaSInfo[i].X.LowerRightY))) * p;
                    //        double Y = Math.Abs(Math.Sqrt((arryAreaSInfo[i].X.LowerLeftX - arryAreaSInfo[i].X.UpperLeftX) * (arryAreaSInfo[i].X.LowerLeftX - arryAreaSInfo[i].X.UpperLeftX) + (arryAreaSInfo[i].X.LowerLeftY - arryAreaSInfo[i].X.UpperLeftY) * (arryAreaSInfo[i].X.LowerLeftY - arryAreaSInfo[i].X.UpperLeftY))) * p;
                    //        A = arryAreaSInfo[i].X.ArryPolluteRatesVar[j].Rate * 1000 / 3600 / (X * Y);
                    //        if (arryAreaSInfo[i].X.Unit == "g/s")
                    //        {
                    //            if ((j + 1) % 3 == 0)
                    //            {
                    //                //每隔3数据就换行
                    //                TempStr += ",  " + "\r\n" + (arryAreaSInfo[i].X.ArryPolluteRatesVar[j].Rate / (X * Y)).ToString("E5");
                    //            }
                    //            else
                    //            {
                    //                TempStr += ",  " + (arryAreaSInfo[i].X.ArryPolluteRatesVar[j].Rate / (X * Y)).ToString("E5");
                    //            }
                    //        }
                    //        else
                    //        {
                    //            if ((j + 1) % 3 == 0)
                    //            {
                    //                TempStr += ",  " + "\r\n" + A.ToString("E5");
                    //            }
                    //            else
                    //            {
                    //                TempStr += ",  " + A.ToString("E5");
                    //            }
                    //        }
                    //    }
                    //    TempStr += "  !" + "\r\n" + "   ! END !" + "\r\n";

                    //    Subgroup += TempStr;

                    //}






                }

            }


            // Subgroup (14c) 没写



            //关于变化速率问题暂时注销



            //string DD = "";
            //for (int i = 0; i < arryAreaSInfo.Count; i++)
            //{
            //    if (arryAreaSInfo[i].SourceVarHouEmiRatBase.IsSourceHour == false)
            //    {
            //        string A = "! SRCNAM  =   " + arryAreaSInfo[i].SRCNAM + " !" + "\r\n";
            //        string B = "!  XVERT = " + Math.Round(arryAreaSInfo[i].X.UpperLeftX, 5) + ",    " + Math.Round(arryAreaSInfo[i].X.UpperRightX, 5) + ",    " + Math.Round(arryAreaSInfo[i].X.LowerRightX, 5) + ",    " + Math.Round(arryAreaSInfo[i].X.LowerLeftX, 5) + "!" + "\r\n";
            //        string C = "!  YVERT = " + Math.Round(arryAreaSInfo[i].X.UpperLeftY, 5) + ",    " + Math.Round(arryAreaSInfo[i].X.UpperRightY, 5) + ",    " + Math.Round(arryAreaSInfo[i].X.LowerRightY, 5) + ",    " + Math.Round(arryAreaSInfo[i].X.LowerLeftY, 5) + "!" + "\r\n" + "!END!" + "\r\n";
            //        DD = DD + A + B + C;
            //    }

            //}






            //Subgroup = Subgroup + DD;





            // Subgroup (14d) 
            string F = "";
            for (int i = 0; i < arryAreaSInfo.Count; i++)
            {

                //if (arryAreaSInfo[i].SourceVarHouEmiRatBase.IsSourceHour == false)
                //{
                //    string D = "";
                //    for (int j = 0; j < arryAreaSInfo[i].X.ArryPolluteRatesVar.Count; j++)
                //    {
                //        if (arryAreaSInfo[i].X.ArryPolluteRatesVar[j].IVARY != 0)
                //        {
                //            string A = "! SRCNAM = " + arryAreaSInfo[i].SRCNAM + " !" + "\r\n";
                //            string B = "! IVARY  =  " + arryAreaSInfo[i].X.ArryPolluteRatesVar[j].IVARY + "  !" + "\r\n";
                //            string C = "";
                //            for (int k = 0; k < arryAreaSInfo[i].X.ArryPolluteRatesVar[j].ArryCoefficient.Count; k++)
                //            {
                //                //C = "";
                //                if (k != arryAreaSInfo[i].X.ArryPolluteRatesVar[j].ArryCoefficient.Count - 1)
                //                {
                //                    C = C + arryAreaSInfo[i].X.ArryPolluteRatesVar[j].ArryCoefficient[k].ToString() + ",";
                //                }
                //                else
                //                {
                //                    C = C + arryAreaSInfo[i].X.ArryPolluteRatesVar[j].ArryCoefficient[k].ToString() + "      !";
                //                }
                //                if ((k + 1) % 6 == 0)
                //                {
                //                    C = C + "\r\n";
                //                }
                //            }
                //            C = "! " + arryAreaSInfo[i].X.ArryPolluteRatesVar[j].PolluteName + "          = " + C + "!END!" + "\r\n";
                //            D = D + A + B + C;
                //        }
                //    }
                //    F = F + D;
                //}

            }
            Subgroup = Subgroup + F;
            return Subgroup;
        }

        public override void ReadFromGroup(Dictionary<string, string> dic)
        {
            foreach (KeyValuePair<string, string> m_dic in dic)
            {
                if (m_dic.Key == "NAR1")
                {
                    this.NAR1 = Convert.ToInt32(m_dic.Value);
                }
                if (m_dic.Key == "IARU")
                {
                    this.IARU = Convert.ToInt32(m_dic.Value);
                }
                if (m_dic.Key == "NSAR1")
                {
                    this.NSAR1 = Convert.ToInt32(m_dic.Value);
                }
                if (m_dic.Key == "NAR2")
                {
                    this.NAR2 = Convert.ToInt32(m_dic.Value);
                }

                AreaSInfo ps = new AreaSInfo();
                List<double> lst = new List<double>();

                if (m_dic.Key == "SRCNAM")
                {
                    ps.SRCNAM = m_dic.Value;

                }
                if (m_dic.Key == "X")
                {
                    lst = StrWriter.ReturnDouList(m_dic.Value);
                    ps.X.EffectHeight = lst[0];
                    ps.X.BaseElevation = lst[1];
                    ps.X.SigmaZ = lst[2];


                    ps.X.ArryPolluteRatesVar.Clear();
                    for (int i = 2; i < lst.Count; i++)
                    {
                        PolluteRatesVar mPolluteRatesVar = new PolluteRatesVar();
                        mPolluteRatesVar.Rate = lst[i];
                        ps.X.ArryPolluteRatesVar.Add(mPolluteRatesVar);
                    }

                }

                Area.Add(ps);


            }

        }



    }




    /// <summary>
    /// 面源排放参数
    /// </summary>
    [Serializable]
    public class AreaSInfo : ICloneable
    {

        /// <summary>
        /// 污染源名称
        /// </summary>
        string m_SRCNAM = "";

        public string SRCNAM
        {
            get { return m_SRCNAM; }
            set { m_SRCNAM = value; }
        }

        private int m_Sid = 0;
        /// <summary>
        ///　污染源编号记录器，只增加，不减少以避免重复，且唯一.
        /// </summary>
        public int Sid
        {
            get { return m_Sid; }
            set { m_Sid = value; }
        }


        private string m_Name = "L";
        /// <summary>
        /// 污染源名称，非Inp文件
        /// </summary>
        public string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }


        /// <summary>
        /// 面源排放参数
        /// </summary>
        AreaXInfo m_X = new AreaXInfo();

        public AreaXInfo X
        {
            get { return m_X; }
            set { m_X = value; }
        }




        //SourceVarHouEmiRatBase m_SourceVarHouEmiRatBase = new SourceVarHouEmiRatBase();
        ///// <summary>
        ///// 小时排放速率参数类
        ///// </summary>
        ///// <returns></returns>
        //public SourceVarHouEmiRatBase SourceVarHouEmiRatBase
        //{
        //    get { return m_SourceVarHouEmiRatBase; }
        //    set { m_SourceVarHouEmiRatBase = value; }
        //}






        public object Clone()
        {
            AreaSInfo obj = new AreaSInfo();
            obj = (AreaSInfo)this.MemberwiseClone();

            AreaXInfo mAreaXInfo = new AreaXInfo();
            mAreaXInfo = (AreaXInfo)this.X.Clone();
            obj.X = mAreaXInfo;


            //SourceVarHouEmiRatBase mSourceVarHouEmiRatBase = new SourceVarHouEmiRatBase();
            //mSourceVarHouEmiRatBase = (SourceVarHouEmiRatBase)this.SourceVarHouEmiRatBase.Clone();
            //obj.SourceVarHouEmiRatBase = mSourceVarHouEmiRatBase;




            return obj;
        }

    }
    /// <summary>
    /// 面源排放参数
    /// </summary>
    [Serializable]
    public class AreaXInfo : ICloneable
    {
        /// <summary>
        /// 面源有效高度，m
        /// </summary>
        double m_EffectHeight = 0;

        public double EffectHeight
        {
            get { return m_EffectHeight; }
            set { m_EffectHeight = value; }
        }
        /// <summary>
        /// 面源基地海拔
        /// </summary>
        double m_BaseElevation = 0;

        public double BaseElevation
        {
            get { return m_BaseElevation; }
            set { m_BaseElevation = value; }
        }
        /// <summary>
        /// 面源的初始垂直扩散系数
        /// </summary>
        double m_SigmaZ = 0;

        public double SigmaZ
        {
            get { return m_SigmaZ; }
            set { m_SigmaZ = value; }
        }

        private double m_UpperLeftX = 0;

        public double UpperLeftX
        {
            get { return m_UpperLeftX; }
            set { m_UpperLeftX = value; }
        }
        private double m_UpperLeftY = 0;

        public double UpperLeftY
        {
            get { return m_UpperLeftY; }
            set { m_UpperLeftY = value; }
        }
        private double m_UpperRightX = 0;

        public double UpperRightX
        {
            get { return m_UpperRightX; }
            set { m_UpperRightX = value; }
        }
        private double m_UpperRightY = 0;

        public double UpperRightY
        {
            get { return m_UpperRightY; }
            set { m_UpperRightY = value; }
        }

        private double m_LowerLeftX = 0;

        public double LowerLeftX
        {
            get { return m_LowerLeftX; }
            set { m_LowerLeftX = value; }
        }
        private double m_LowerLeftY = 0;

        public double LowerLeftY
        {
            get { return m_LowerLeftY; }
            set { m_LowerLeftY = value; }
        }
        private double m_LowerRightX = 0;

        public double LowerRightX
        {
            get { return m_LowerRightX; }
            set { m_LowerRightX = value; }
        }
        private double m_LowerRightY = 0;

        public double LowerRightY
        {
            get { return m_LowerRightY; }
            set { m_LowerRightY = value; }
        }
        private string m_Unit = "g/s";//单位
        /// <summary>
        /// 单位
        /// </summary>
        public string Unit
        {
            get { return m_Unit; }
            set { m_Unit = value; }
        }

        private List<PolluteRatesVar> m_arryPolluteRatesVar = new List<PolluteRatesVar>();
        /// <summary>
        /// 污染物速率相关的一此参数
        /// </summary>
        public List<PolluteRatesVar> ArryPolluteRatesVar
        {
            get { return m_arryPolluteRatesVar; }
            set { m_arryPolluteRatesVar = value; }
        }
        public object Clone()
        {
            AreaXInfo obj = new AreaXInfo();
            obj = (AreaXInfo)this.MemberwiseClone();
            List<PolluteRatesVar> arryPollute = new List<PolluteRatesVar>();
            for (int i = 0; i < this.ArryPolluteRatesVar.Count; i++)
            {
                arryPollute.Add((PolluteRatesVar)this.ArryPolluteRatesVar[i].Clone());
            }
            obj.ArryPolluteRatesVar = arryPollute;
            return obj;
        }
    }





}
